package com.jplus.controller;

import com.jplus.VO.ResultVO;
import com.jplus.entity.Comments;
import com.jplus.entity.Loding;
import com.jplus.entity.Topic;
import com.jplus.entity.User;
import com.jplus.handler.GlobalExceptionHandler;
import com.jplus.service.CommentService;
import com.jplus.utils.ResultVOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
public class CommentController extends GlobalExceptionHandler {

    @Autowired
    private CommentService commentService;

    @RequestMapping(value = "/commentPage", method = RequestMethod.GET)
    public ResultVO goToCommentPage() {
        return commentService.getTopicBeforeGoToCommentPage();
    }

    @RequestMapping(value = "/comment", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    public ResultVO submitComment(Comments comments, Topic topic, HttpSession session) {
        // 获取用户数据
        User user = (User) session.getAttribute("customer");
        if (user == null) {
            Loding loding = new Loding();
            loding.setLodingMessage("请先登录再评论");
            loding.setLodingUrl("/loginPage");
            return ResultVOUtils.error(0, "未登录", loding);
        } else {
           return commentService.submitComment(comments, topic, user);
        }
    }
}
